package com.ecloud.support.generator.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.common.base.result.Result;
import com.ecloud.common.base.result.ResultBuilder;
import com.ecloud.common.base.result.ResultStatus;
import com.ecloud.common.base.result.RowsBuilder;
import com.ecloud.common.utils.check.LongUtils;
import com.ecloud.common.utils.check.StringUtils;
import com.ecloud.common.utils.code.UUIDUtil;
import com.ecloud.support.generator.mapper.TemplateMapper;
import com.ecloud.support.generator.model.convert.TemplateConverter;
import com.ecloud.support.generator.model.dto.TemplateDto;
import com.ecloud.support.generator.model.entity.TemplatePo;
import com.ecloud.support.generator.model.vo.TemplateVo;
import com.ecloud.support.generator.service.iface.ITemplateService;

/**
 * TemplateService
 *
 * @author LiuGangQiang Create in 2021/04/15
 */
@Service
public class TemplateService extends ServiceImpl<TemplateMapper, TemplatePo> implements ITemplateService {

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateService#queryList(com.ecloud.support.generator.model.dto.TemplateDto)
     */
    @Override
    public Result<?> queryList(TemplateDto dto) {
        /* 构造查询条件 */
        LambdaQueryWrapper<TemplatePo> queryWrapper = new QueryWrapper<TemplatePo>().lambda();
        queryWrapper.eq(StringUtils.isNotBlank(dto.getTemplateSetId()), TemplatePo::getTemplateSetId, dto.getTemplateSetId());
        queryWrapper.and(StringUtils.isNotBlank(dto.getKeyword()), wrapper -> wrapper.like(TemplatePo::getTemplateName, dto.getKeyword()));
        queryWrapper.orderByAsc(TemplatePo::getSort);
        /* 构造分页查询 */
        IPage<TemplatePo> page = this.page(new Page<>(dto.getPage(), dto.getRows()), queryWrapper);
        if (LongUtils.isGtZero(page.getTotal())) {
            /* 转换 */
            List<TemplateVo> pos = TemplateConverter.INSTANCE.poToVo(page.getRecords());
            return ResultBuilder.build(ResultStatus.OK, RowsBuilder.build(page.getTotal(), pos));
        }
        return ResultBuilder.build(ResultStatus.DATA_NOT_FOUND, RowsBuilder.build());
    }

    /**
     * @see com.ecloud.support.generator.service.iface.ITemplateService#insertBatch(java.util.List)
     */
    @Override
    public Result<?> insertBatch(List<TemplateDto> dtos) {
        /* 先删除所有记录 */
        LambdaQueryWrapper<TemplatePo> deleteWrapper = new LambdaQueryWrapper<TemplatePo>();
        deleteWrapper.in(TemplatePo::getTemplateSetId, dtos.stream().map(TemplateDto::getTemplateSetId).collect(Collectors.toList()));
        this.baseMapper.delete(deleteWrapper);
        List<TemplatePo> templates = TemplateConverter.INSTANCE.dtoToPo(dtos);
        /* 循环设置ID */
        templates.stream().forEach(template -> {
            template.setId(UUIDUtil.uuid());
        });
        if (this.saveBatch(templates)) {
            return ResultBuilder.build(ResultStatus.OK);
        }
        return ResultBuilder.build(ResultStatus.FAIL);
    }

}
